package com.ibp.entity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServiceNode {
	private String serviceNode;
	private String serviceNodeSearch ;
	private List<HostMachine> hostMachines;
	private String hostMachineAddressString;
	private String hostMachineAddressSearch;

	public String getServiceNode() {
		return this.serviceNode;
	}

	public void setServiceNode(String serviceNode) {
		this.serviceNode = serviceNode;
	}
	
	public String getServiceNodeSearch() {
		return serviceNodeSearch;
	}

	public void setServiceNodeSearch(String serviceNodeSearch) {
		this.serviceNodeSearch = serviceNodeSearch;
	}

	public List<HostMachine> getHostMachines() {
		return hostMachines;
	}

	public void setHostMachines(List<HostMachine> hostMachines) {
		this.hostMachines = hostMachines;
	}

	public String getHostMachineAddressString() {
		return this.hostMachineAddressString;
	}

	public void setHostMachineAddressString(String hostMachineAddressString) {
		this.hostMachineAddressString = hostMachineAddressString;
	}

	public String getHostMachineAddressSearch() {
		return hostMachineAddressSearch;
	}

	public void setHostMachineAddressSearch(String hostMachineAddressSearch) {
		this.hostMachineAddressSearch = hostMachineAddressSearch;
	}

	/***
	 * 获取当前节点的状态
	 * @return
	 */
	public ServiceStatusEnum getServiceNodeStatus(){
	    ServiceStatusEnum nodeIbmaStatus = getServiceNodeIbmaStatus();
	    ServiceStatusEnum nodeIbasStatus = getServiceNodeIbasStatus();
		if(nodeIbmaStatus.equals(ServiceStatusEnum.START) 
				&& (nodeIbasStatus.equals(ServiceStatusEnum.START)/* || nodeIbasStatus.equals(ServiceStatusEnum.NULL_START)*/)){
			return ServiceStatusEnum.START ;
		}else if (nodeIbmaStatus.equals(ServiceStatusEnum.STOP)
				&& (nodeIbasStatus.equals(ServiceStatusEnum.STOP) /*|| nodeIbasStatus.equals(ServiceStatusEnum.NULL_STOP)*/)){
			return ServiceStatusEnum.STOP ;
		}else{
			return ServiceStatusEnum.UNKNOWN ;
		}
		
	}
	
	/***
	 * 获取当前节点下服务代理（ibma）的集体状态
	 * @return
	 */
	public ServiceStatusEnum getServiceNodeIbmaStatus(){
		int hmCount = hostMachines.size() ;
		Map<String,Integer> m = new HashMap<String,Integer>();
		for(HostMachine hm : hostMachines){
			String hostIbmaStatus = hm.getIbmaStatus().toString() ;
			getStatusMap(m,hostIbmaStatus);
		}
		
		if(m.get(ServiceStatusEnum.START.toString()) != null  && m.get(ServiceStatusEnum.START.toString()) == hmCount){
			return ServiceStatusEnum.START ;
		}else if (m.get(ServiceStatusEnum.STOP.toString()) != null && m.get(ServiceStatusEnum.STOP.toString()) == hmCount){
			return ServiceStatusEnum.STOP ;
		}else{
			return ServiceStatusEnum.UNKNOWN ;
		}
		
	}
	
	/***
	 * 获取当前节点下通讯服务端主机（ibas）的集体状态
	 * @return
	 */
	public ServiceStatusEnum getServiceNodeIbasStatus(){
		int hmCount = hostMachines.size() ;
		Map<String,Integer> m = new HashMap<String,Integer>();
		for(HostMachine hm : hostMachines){
			String hostIbasStatus = hm.getIbasStatus().toString() ;
			getStatusMap(m,hostIbasStatus);
		}
		
		if(m.get(ServiceStatusEnum.START.toString()) != null  && m.get(ServiceStatusEnum.START.toString()) == hmCount){
			return ServiceStatusEnum.START ;
		}else if (m.get(ServiceStatusEnum.STOP.toString()) != null && m.get(ServiceStatusEnum.STOP.toString()) == hmCount){
			return ServiceStatusEnum.STOP ;
		}else if (m.get(ServiceStatusEnum.NULL.toString()) != null && m.get(ServiceStatusEnum.NULL.toString()) == hmCount){
			return ServiceStatusEnum.NULL ;
		}else if (m.get(ServiceStatusEnum.START.toString()) != null
				&& m.get(ServiceStatusEnum.NULL.toString()) != null
				&& (m.get(ServiceStatusEnum.START.toString())+m.get(ServiceStatusEnum.NULL.toString())) == hmCount){
			return ServiceStatusEnum.NULL_START ;
		}else if (m.get(ServiceStatusEnum.STOP.toString()) != null
				&& m.get(ServiceStatusEnum.NULL.toString()) != null
				&& (m.get(ServiceStatusEnum.STOP.toString()) + m.get(ServiceStatusEnum.NULL.toString())) == hmCount){
			return ServiceStatusEnum.NULL_STOP ;
		}else{
			return ServiceStatusEnum.UNKNOWN ;
		}
	}
	
	protected void getStatusMap(Map<String,Integer> m,String status){
		if(status != null){
			if(m.containsKey(status)){
				m.put(status, m.get(status)+1);
			}else{
				m.put(status, 1);
			}
		}else{
			if(m.containsKey(ServiceStatusEnum.UNKNOWN.toString())){
				m.put(ServiceStatusEnum.UNKNOWN.toString(), m.get(ServiceStatusEnum.UNKNOWN.toString())+1);
			}else{
				m.put(ServiceStatusEnum.UNKNOWN.toString(), 1);
			}
		}
	}
	
	@Override
	public String toString() {
		return "ServiceNode [serviceNode=" + serviceNode
				+ ", hostMachineAddress=" + hostMachines
				+ ", hostMachineAddressString=" + hostMachineAddressString
				+ ", hostMachineAddressSearch=" + hostMachineAddressSearch
				+ "]";
	}

}